﻿using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using CoreMobile.cfg;
using VABMobile.cfg;
using VABMobile.oo;

namespace VABMobile.ui
{
    public partial class ServiceNameForm : Form
    {
        private DateTime internalDate;

        public ServiceNameForm()
        {
            InitializeComponent();

            loadDeafultData();

            //AllKeys(true);
        }

        //[DllImport("coredll.dll", SetLastError = true)]
        //static extern bool AllKeys(bool bAllKeys);

        private void loadDeafultData()
        {
            this.business.Enabled = false;
            this.business.Text = SettingsManager<VABSettings>.get().Settings.EnterpriseName;

            foreach (var itinerario in Itinerario.itinerarios())
            {
                this.services.Items.Add(itinerario.Codigo);
            }
            this.services.SelectedIndex = 0;
            this.ahead.Checked = true;
            resetDate();
        }

        private void resetDate()
        {   
            var now = DateTime.Now;
            this.date.Text = now.ToString("dd/MM/yyyy");
            this.hourTime.Text = now.ToString("HH");
            this.minTime.Text = now.ToString("mm");
            this.setInternalDate();
        }


        private bool setInternalDate()
        {
            try
            {

                internalDate = DateTime.ParseExact(
                    this.date.Text + " " + 
                    hourTime.Text+":"+minTime.Text
                , "dd/MM/yyyy H:m", null);
            }
            catch (FormatException)
            {

                return false;
            }
            return true;
        }

        public string ServiceName
        {
            get
            {
                return this.business.Text
                       + this.SelectedServiceName
                       + this.internalDate.ToString("yyyyMMddHHmm");
            }
        }


        private void validateButton_Click(object sender, EventArgs e)
        {
            
            if (! setInternalDate())
            {
                MessageBox.Show("Formato fecha, hora debe ser: D/M/A , H:m");
                return;
            }
            this.serviceName.Text = this.ServiceName;
            this.date.Enabled = false;
            this.hourTime.Enabled = false;
            this.services.Enabled = false;
            this.validateButton.Enabled = false;

            this.okButton.Enabled = true;
        }

        public string SelectedServiceName
        {
            get
            {
                return this.services.SelectedItem.ToString()
                       + (this.ahead.Checked
                           ? "I"
                           : "V");
            }
        }

        private void returning_CheckedChanged(object sender, EventArgs e)
        {
            this.ahead.Checked = false;
            //this.returning.Checked = true;
        }

        private void ahead_CheckedChanged(object sender, EventArgs e)
        {
            this.returning.Checked = false;
           // this.ahead.Checked = true;
        }

        private void calendar_DateChanged(object sender, DateRangeEventArgs e)
        {
            this.internalDate = calendar.SelectionStart;
            this.date.Text = internalDate.ToString("dd/MM/yyyy");
            calendar.Hide();
            dateButton.Enabled = true;
        }

        private void dateButton_Click(object sender, EventArgs e)
        {
            dateButton.Enabled = false;
            calendar.Show();
        }

        private void ServiceNameForm_KeyUp(object sender, KeyEventArgs e)
        {
            /*
            if(e.KeyCode != (Keys)117) return;

            this.

            Keys a = e.KeyCode;
            */
        }

    }
}